﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace atmsystem_ss
{
    public partial class frmBack_PIN : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable("tblAccount");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;


        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }

        public frmBack_PIN()
        {
            InitializeComponent();
        }

        private void frmBack_PIN_Load(object sender, EventArgs e)
        {
            connect();
        }
        private bool kiemtra() {
            kt = true;
            
            if (txtID_Account.Text == "")
            {
                txtID_Account.Focus();
                MessageBox.Show("Ban chua nhap Account ID !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtPIN.Text == "")
            {
                txtPIN.Focus();
                MessageBox.Show("Ban chua nhap PIN !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
               
            else if (Convert.ToString(txtPIN.Text).Length.Equals(4) == false)
            {
                txtPIN.Focus();
                MessageBox.Show("Chu y PIN co 4 ky tu, moi nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }
        private void btncomplete_Click(object sender, EventArgs e)
        {
            dt.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblAccount where fldID_Account LIKE '%'+@fldID_Account+'%' ";
            command.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
            da.SelectCommand = command;
            da.Fill(dt);
            kiemtra();
            if (kt == true)
            {
                if (dt.Rows.Count > 0)
                {
                    DataRow row = dt.Select("fldID_Account = '" + Convert.ToString(txtID_Account.Text) + "'")[0];
                    row.BeginEdit();
                    row["fldPIN"] = txtPIN.Text;
                    row.EndEdit();
                    SqlCommand commandUpdate = new SqlCommand();
                commandUpdate.Connection = con;
                commandUpdate.CommandType = CommandType.Text;
                commandUpdate.CommandText = @"update tblAccount set 
                                                                fldPIN = @fldPIN
                                                                where fldID_Account= @fldID_Account";
                commandUpdate.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50, "fldID_Account");
                commandUpdate.Parameters.Add("@fldPIN", SqlDbType.NVarChar, 50, "fldPIN");
                da.UpdateCommand = commandUpdate;
                da.Update(dt);
                    MessageBox.Show("Complete", "Thong bao", MessageBoxButtons.OK,MessageBoxIcon.Information);
                    txtID_Account.Text = "";
                    txtPIN.Text = "";
                }
                else
                {
                    if (MessageBox.Show("Account ID khong ton tai", "Thong bao", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                    {
                        txtID_Account.Text = "";
                        txtID_Account.Focus();
                        txtPIN.Text = "";
                    }
                    else
                    {
                        Close();
                        Dispose();
                        disconnect();
                        frmAdmin _frmAdmin = new frmAdmin();
                        _frmAdmin.Show();
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmAdmin _frmAdmin = new frmAdmin();
            _frmAdmin.Show();
        }

       
        
    }
}
